package com.wsoft.portal.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageInfo;
import com.wsoft.core.exception.Asserts;
import com.wsoft.core.utils.LoginUserUtil;
import com.wsoft.portal.entity.PortalOrgMemberEntity;
import com.wsoft.portal.manager.PortalOrgMemberManager;
import com.wsoft.portal.mapper.PortalOrgMemberMapper;
import com.wsoft.portal.query.org.member.OrgMemberInsertQuery;
import com.wsoft.portal.query.org.member.OrgMemberListQuery;
import com.wsoft.portal.query.org.member.OrgMemberUpdateQuery;
import com.wsoft.portal.service.IPortalOrgMemberService;
import com.wsoft.portal.utils.PortalUtil;
import com.wsoft.portal.vo.org.member.OrgMemberVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 空间类型 服务实现类
 * </p>
 *
 * @author Z001
 * @since 2024年12月27日 10:01:32
 */
@Service
public class PortalOrgMemberServiceImpl extends ServiceImpl<PortalOrgMemberMapper, PortalOrgMemberEntity> implements IPortalOrgMemberService {

    @Resource
    private PortalOrgMemberManager orgMemberManager;
    @Resource
    private PortalUtil portalUtil;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean insertOrgMember(OrgMemberInsertQuery query) {
        if (null == query.getMemberId() && null == query.getOrgId()) {
            //组织和用户不能同时为空
            Asserts.fail("[portal.org.member.not.null]");
        }
        checkTheme(query.getThemeId(), query.getOrgId(), query.getMemberId(), null);

        //复制
        Long curOrgId = query.getOrgId();
        if (null != query.getMemberId()) {
            curOrgId = LoginUserUtil.getUserInfo().getLoginUserVO().getLoginOrgId();
        }
        //递归获取上级节点
        PortalOrgMemberEntity parPortal = portalUtil.getParentPortal(query.getThemeId(), curOrgId);
        //复制的时候 会新增,这边代码不在重复新增
        portalUtil.copyDefaultTheme(query.getThemeId(), parPortal, query.getOrgId(), query.getMemberId());
        return true;
    }

    @Override
    public boolean updateOrgMember(OrgMemberUpdateQuery query) {
        checkTheme(query.getThemeId(), query.getOrgId(), query.getMemberId(), query.getId());
        return orgMemberManager.updateById(Convert.convert(PortalOrgMemberEntity.class, query));
    }

    @Override
    public OrgMemberVO getOrgMember(Long id) {
        return orgMemberManager.getOrgMemberInfo(id);
    }

    @Override
    public PageInfo<OrgMemberVO> listOrgMember(OrgMemberListQuery query) {
        return orgMemberManager.pageOrgMember(query);
    }

    @Override
    public boolean deleteOrgMember(Long id) {
        return orgMemberManager.removeById(id);
    }

    // ################################## 私有方法  ##################################

    /**
     * 获取上级portal
     * @param themeId
     * @param parOrgId
     * @return
     */
    private PortalOrgMemberEntity getParPortal(Long themeId, List<Long> parOrgId) {
        for (Long orgId : parOrgId) {
            PortalOrgMemberEntity portal = orgMemberManager.getByThemeId(themeId, orgId, null);
            if (null != portal) {
                return portal;
            }
        }
        return null;
    }

    /**
     * 校验主题 在组织/用户下是否已存在
     *
     * @param themeId  主题id
     * @param orgId    组织id
     * @param memberId 用户id
     * @param id
     */
    private void checkTheme(Long themeId, Long orgId, Long memberId, Long id) {
        PortalOrgMemberEntity entity = orgMemberManager.getByThemeId(themeId, orgId, memberId);
        //新增
        if (null == id && entity != null) {
            Asserts.fail("[portal.theme.exist]");
        }
        //编辑
        if (null != id && entity != null && !id.equals(entity.getId())) {
            Asserts.fail("[portal.theme.exist]");
        }
    }

}
